#include "keyboardview.h"

KeyBoardView::KeyBoardView(QWidget * parent)
    : QWidget(parent)
{
    this->setObjectName("KeyBoardView");
    this->setStyleSheet("#KeyBoardView{background-color: #000000;}");
}

void KeyBoardView::setUI()
{
    int X = 25;
    int Y = 20;

    QString styleSheet = "background-color: qlineargradient(x1:0, y1:0, x2:0, y2:1, stop:0 #0973AB,stop:1 #098DCE)";
    QString key =  QString("1234567890ABCDEFGHJKLMNOPQRSTWXYZ.-");
    const int lineNum = 10;
    const int btnW = 68;
    const int btnH = 64;
    const int spacing = 8;
    int line, col;
    int i = 0;
    for (i = 0; i < key.length(); ++i) {
        line = i / lineNum;
        col = i % lineNum;
        BmpButton* btn = new BmpButton(this);
        btn->setGeometry(X+(btnW+spacing)*col, Y+(btnH+spacing)*line, btnW, btnH);
        BMPBUTTON_INFO b;
        //        b.bmpPath = QString("input_btn01_u");
        //        b.bmpPathPressed = QString("input_btn01_d");
        //        b.bmpPathX = QString("input_btn01_x");
        b.text = key.mid(i, 1);
        b.txtColor = QColor(255, 255, 255);
        btn->setUI(b);
        btn->setStyleSheet(styleSheet);
        mListBtn.push_back(btn);
    }

    line = i / lineNum;
    col = i % lineNum;
    BmpButton* btnBlank = new BmpButton(this);
    btnBlank->setGeometry(X+(btnW+spacing)*col, Y+(btnH+spacing)*line, btnW, btnH);
    BMPBUTTON_INFO b;
    b.fontSize = 8;
    b.text = "Blank";
    b.txtColor = QColor(255, 255, 255);
    b.txtColorPressed = QColor(255, 255, 0);
    btnBlank->setUI(b);
    btnBlank->setStyleSheet(styleSheet);
    mListBtn.push_back(btnBlank);

    i++;
    line = i / lineNum;
    col = i % lineNum;
    BmpButton* btnBackspace = new BmpButton(this);
    btnBackspace->setGeometry(X+(btnW+spacing)*col, Y+(btnH+spacing)*line, btnW, btnH);
    b.revert();
    //    b.icon = 0xf177;
    b.fontSize = 8;
    b.text = "Backspace";
    b.txtColor = QColor(255, 255, 255);
    b.txtColorPressed = QColor(255, 255, 0);
    btnBackspace->setUI(b);
    btnBackspace->setStyleSheet(styleSheet);
    mListBtn.push_back(btnBackspace);

    i++;
    line = i / lineNum;
    col = i % lineNum;
    BmpButton* btnClear = new BmpButton(this);
    btnClear->setGeometry(X+(btnW+spacing)*col, Y+(btnH+spacing)*line, btnW, btnH);
    b.revert();
    //    b.icon = 0xf177;
    b.fontSize = 8;
    b.text = "Clear";
    b.txtColor = QColor(255, 255, 255);
    b.txtColorPressed = QColor(255, 255, 0);
    btnClear->setUI(b);
    btnClear->setStyleSheet(styleSheet);
    mListBtn.push_back(btnClear);

    i++;
    line = i / lineNum;
    col = i % lineNum;
    BmpButton* btnCapsLk = new BmpButton(this);
    btnCapsLk->setGeometry(X+(btnW+spacing)*col, Y+(btnH+spacing)*line, btnW, btnH);
    b.revert();
    //    b.icon = 0xf177;
    b.fontSize = 8;
    b.text = "CapsLk";
    b.txtColor = QColor(255, 255, 255);
    b.txtColorPressed = QColor(255, 255, 0);
    btnCapsLk->setUI(b);
    btnCapsLk->setStyleSheet(styleSheet);
    mListBtn.push_back(btnCapsLk);

    i++;
    line = i / lineNum;
    col = i % lineNum;
    BmpButton* btnEnter = new BmpButton(this);
    btnEnter->setGeometry(X+(btnW+spacing)*col, Y+(btnH+spacing)*line, btnW, btnH);
    b.revert();
    //    b.icon = 0xe74c;
    b.fontSize = 8;
    b.text = "Enter";
    b.txtColor = QColor(255, 255, 255);
    b.txtColorPressed = QColor(255, 255, 0);
    btnEnter->setUI(b);
    btnEnter->setStyleSheet(styleSheet);
    mListBtn.push_back(btnEnter);

    setSlot();

}

void KeyBoardView::setSlot()
{
    for (int i = 0; i < mListBtn.size(); ++i) {
        QObject::connect(mListBtn.at(i), SIGNAL(sendKey(QString)), this->parentWidget(), SLOT(inputKey(QString)));
    }
}

void KeyBoardView::onCapsLK()
{
    isUppercase = !isUppercase;
    for (int i = 10; i < mListBtn.size() - 4; ++i) {
        BMPBUTTON_INFO b = mListBtn.at(i)->getUI();
        QString strKey = "";
        if (isUppercase) {
            strKey = b.text.toUpper();
        } else {
            strKey = b.text.toLower();
        }
        mListBtn.at(i)->setText(strKey, true);
    }
}
